package com.u3k.app.mid;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import com.u3k.app.mid.core.IModule;
import com.u3k.app.mid.core.ModuleManager;
import com.u3k.app.mid.utils.common.PathUtil;
import com.u3k.app.mid.utils.common.Util;
import com.u3k.app.mid.utils.statistics.StatisticsManager;
import com.u3k.mid.*;
import com.u3k.mid.StartActionReceiver;

import java.util.ArrayList;
import java.util.Iterator;

public class MiddleMain {
    public static final String ACTION_APPID = "appid";
    public static final String ACTION_CHANNELID = "channelid";
    public static final String ACTION_MIDDLE_HASH = "middle.hash";
    public static final String MIDDLE_HAVE_START_ACTION = "action.mid.have.start";
    public static final String MIDDLE_START_ACTION = "action.mid.start";
    public static String TAG = "MiddleMain";
    static MiddleMain mInstance;
    public String mAppId;
    public String mChannelId;
    public Context mContext;
    public boolean mEnableStart;
    public Handler mHandler = new Handler(Looper.getMainLooper());
    String mShellVersion;
    public StartActionReceiver mStartReceiver;

    public static MiddleMain getInstance() {
        synchronized (MiddleMain.class) {
            String str;
            try {
                str = "getInstance";
                LogUtils.LogError(TAG, str);
                if (mInstance == null) {
                    mInstance = new MiddleMain();
                }
                MiddleMain middleMain = mInstance;
                return middleMain;
            } finally {
                str = MiddleMain.class;
            }
        }
    }

    private void initInnerData(String[][] strArr) {
        if (strArr.length > 0) {
            ab.a = strArr[0];
        }
        if (strArr.length > 1) {
            ab.b = strArr[1];
        }
        if (strArr.length > 2) {
            ab.c = strArr[2];
        }
        if (strArr.length > 3) {
            ab.d = strArr[3];
        }
        if (strArr.length > 4) {
            ab.e = strArr[4];
        }
        if (strArr.length > 5) {
            ab.f = strArr[5];
        }
        if (strArr.length > 6) {
            ab.g = strArr[6];
        }
        if (strArr.length > 7) {
            ab.h = strArr[7];
        }
        if (strArr.length > 8) {
            ab.i = strArr[8];
        }
        if (strArr.length > 9) {
            ab.j = strArr[9];
        }
    }

    public void initModules(Context context) {
        LogUtils.LogError(TAG, "initModules");
        try {
            ArrayList moduleInfos = ModuleManager.getInstance(context).getModuleInfos();
            if (moduleInfos != null && !moduleInfos.isEmpty()) {
                Iterator it = moduleInfos.iterator();
                while (it.hasNext()) {
                    ModuleInfo uVar = (ModuleInfo) it.next();
                    if (uVar.isEnable == -1) {
                        String a = v.a(this.mContext, uVar.name);
                        LogUtils.LogError(TAG, "initModules  delete module:" + a);
                        PathUtil.deleteFiles(a);
                    } else if (uVar.isEnable != 1) {
                        LogUtils.LogError(TAG, "initModules  module=" + uVar.name + ", enable=" + uVar.isEnable);
                    } else {
                        IModule module = ModuleManager.getInstance(context).getModule(context, uVar.name);
                        if (module != null) {
                            LogUtils.LogError(TAG, "initModules  init module:" + uVar.name);
                            ModuleManager.getInstance(context).moduleInit(module, context);
                            if (TextUtils.isEmpty(uVar.e)) {
                                uVar.e = module.getVersion();
                                uVar.a(context);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            LogUtils.LogError(TAG, "initModules e=" + e);
            StatisticsManager.getInstance().logException(this.mContext, e);
        }
    }

    private void listenStart(Context context) {
        if (this.mStartReceiver == null) {
            this.mStartReceiver = new StartActionReceiver(this);
        }
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(MIDDLE_START_ACTION);
        intentFilter.addAction(MIDDLE_HAVE_START_ACTION);
        context.registerReceiver(this.mStartReceiver, intentFilter);
    }

    public void sendHaveStartAction(Context context) {
        Intent intent = new Intent(MIDDLE_HAVE_START_ACTION);
        intent.putExtra(ACTION_APPID, this.mAppId);
        intent.putExtra(ACTION_CHANNELID, this.mChannelId);
        intent.putExtra(ACTION_MIDDLE_HASH, hashCode());
        context.sendBroadcast(intent);
    }

    private void sendStartAction(Context context) {
        Intent intent = new Intent(MIDDLE_START_ACTION);
        intent.putExtra(ACTION_APPID, this.mAppId);
        intent.putExtra(ACTION_CHANNELID, this.mChannelId);
        intent.putExtra(ACTION_MIDDLE_HASH, hashCode());
        context.sendBroadcast(intent);
    }

    private void startInitModules() {
        new Thread(new k(this)).start();
    }

    public boolean checkRegister(Context context) {
        LogUtils.LogError(TAG, "checkRegister");
        if (Util.checkRegistered(context)) {
            return true;
        }
        new Thread(new g(this, context)).start();
        return false;
    }

    public String getAppId() {
        return this.mAppId;
    }

    public String getChannelId() {
        return this.mChannelId;
    }

    public String getShellVersion() {
        return this.mShellVersion;
    }

    public void init(Object[] objArr) {
        try {
            Context context = (Context) objArr[0];
            String appId = (String) objArr[1];
            String channelId = (String) objArr[2];
            String hexStr = (String) objArr[3];
            String str4 = (String) objArr[4];//she1.1.0
            String str5 = (String) objArr[5];//U3kSDK_she1.1.0_1"
            initInnerData((String[][]) objArr[6]);//com.u3k.sdk.j.
            LogUtils.LogError(TAG, "init  appId=" + appId + ", channelId=" + channelId + ", appIdKey=" + hexStr);
            this.mEnableStart = true;
            PathUtil.initAppIdKey(hexStr);
            this.mContext = context.getApplicationContext();
            this.mAppId = appId;
            this.mChannelId = channelId;
            this.mShellVersion = str4;
            ae.initApplication(this.mContext);
            ExternalApiManager.a(context);
            Util.saveAppId(context, appId);
            Util.saveChannelId(context, channelId);
            Util.saveShellVersion(context, str4);
            Util.saveBundleId(context, str5);
            listenStart(this.mContext);
            sendStartAction(this.mContext);
            d.getInstance().a(context);//delete files in temp_lib
            this.mHandler.postDelayed(new j(this), 2000);
        } catch (Exception e) {
            LogUtils.LogError(TAG, "MiddleMain init e=" + e);
            StatisticsManager.getInstance().logException(this.mContext, e);
        }
    }
}
